﻿namespace Cmn.Mis
{
    using Cmn;
    using Cmn.Cls;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class TableOpt
    {
        private static string CastValue(string value)
        {
            if (value.Trim() == "")
            {
                return "null";
            }
            return ("'" + Safe.sqlStr(value) + "'");
        }

        private static int CheckTableInfo(TableInfo tableInfo)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            ArrayList list = DB.getTableAllField(tableInfo.TableName);
            ArrayList list2 = new ArrayList();
            if (tableInfo.KeyField == null)
            {
                tableInfo.KeyField = list[0].ToString();
            }
            foreach (KeyValuePair<string, string> pair in tableInfo.FieldValueList)
            {
                if (list.IndexOf(pair.Key) == -1)
                {
                    list2.Add(pair.Key);
                }
            }
            for (int i = 0; i < list2.Count; i++)
            {
                tableInfo.FieldValueList.Remove(list2[i].ToString());
            }
            return 0;
        }

        public static int Delete(TableInfo tableInfo, string keyValue)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            if ((tableInfo.KeyField == null) || tableInfo.KeyField.Trim().Equals(""))
            {
                tableInfo.KeyField = DB.getTableKey(tableInfo.TableName);
            }
            if (tableInfo.KeyField.Trim().Equals(""))
            {
                return -2;
            }
            if (!DB.execSql("delete from " + tableInfo.TableName + (keyValue.Trim().Equals("") ? "" : (" where " + tableInfo.KeyField + "=" + keyValue))))
            {
                return -6;
            }
            return 0;
        }

        public static int DeleteWhere(TableInfo tableInfo, string _DeleteWhere)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            if ((_DeleteWhere != null) && (_DeleteWhere != ""))
            {
                _DeleteWhere = !_DeleteWhere.Trim().Substring(0, 5).ToLower().Equals("where ") ? (" where " + _DeleteWhere) : _DeleteWhere;
            }
            if (!DB.execSql("delete from " + tableInfo.TableName + _DeleteWhere))
            {
                return -6;
            }
            return 0;
        }

        public static int GetTableInfo(TableInfo tableInfo, string keyValue)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            if ((tableInfo.KeyField == null) || tableInfo.KeyField.Trim().Equals(""))
            {
                tableInfo.KeyField = DB.getTableKey(tableInfo.TableName);
            }
            if (tableInfo.KeyField.Trim().Equals(""))
            {
                return -2;
            }
            DataTable table = DB.getDataTable("select * from " + tableInfo.TableName + " where " + tableInfo.KeyField + "='" + keyValue + "'");
            if (table.Rows.Count <= 0)
            {
                return -1;
            }
            for (int i = 0; i <= (table.Columns.Count - 1); i++)
            {
                tableInfo.FieldValueList.Add(table.Columns[i].ColumnName.Trim().ToLower(), table.Rows[0].ItemArray.GetValue(i).ToString());
            }
            return 0;
        }

        public static int GetTableInfo(TableInfo tableInfo, GridView gridView)
        {
            string str = "";
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                if ((gridView.Columns[i] is BoundField) && !tableInfo.FieldValueList.TryGetValue(((BoundField) gridView.Columns[i]).DataField.ToLower(), out str))
                {
                    str = gridView.SelectedRow.Cells[i + 1].Text.Trim();
                    if (str == "&nbsp;")
                    {
                        str = "";
                    }
                    tableInfo.FieldValueList.Add(((BoundField) gridView.Columns[i]).DataField.ToLower(), str);
                }
            }
            return 0;
        }

        public static void GetTableInfo(Control control, TableInfo tableInfo)
        {
            UI.GetValueList(tableInfo.FieldValueList, control);
        }

        public static int GetTableInfo(TableInfo tableInfo, DataTable dataTable, int rowIndex)
        {
            string str = "";
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                if (!tableInfo.FieldValueList.TryGetValue(dataTable.Columns[i].ColumnName.ToLower(), out str))
                {
                    tableInfo.FieldValueList.Add(dataTable.Columns[i].ColumnName.ToLower(), dataTable.Rows[rowIndex][i].ToString());
                }
            }
            return 0;
        }

        public static int Insert(TableInfo tableInfo)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            CheckTableInfo(tableInfo);
            string str = string.Empty;
            string str2 = string.Empty;
            foreach (KeyValuePair<string, string> pair in tableInfo.FieldValueList)
            {
                str = str + pair.Key.ToString() + ",";
                str2 = str2 + CastValue(pair.Value) + ",";
            }
            str = str.Trim().TrimEnd(new char[] { ',' });
            str2 = str2.Trim().TrimEnd(new char[] { ',' });
            if (!DB.execSql("insert into " + tableInfo.TableName + "(" + str + ") values(" + str2 + ")"))
            {
                return -6;
            }
            return 0;
        }

        public static int Insert(TableInfo tableInfo, ref string identity)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            CheckTableInfo(tableInfo);
            string str = string.Empty;
            string str2 = string.Empty;
            foreach (KeyValuePair<string, string> pair in tableInfo.FieldValueList)
            {
                str = str + pair.Key.ToString() + ",";
                str2 = str2 + CastValue(pair.Value) + ",";
            }
            str = str.Trim().TrimEnd(new char[] { ',' });
            str2 = str2.Trim().TrimEnd(new char[] { ',' });
            string str3 = "";
            tableInfo.FieldValueList.TryGetValue(tableInfo.KeyField.ToLower(), out str3);
            string strSql = "insert into " + tableInfo.TableName + "(" + str + ") values(" + str2 + ") \r\n                select case when @@IDENTITY is null then '" + str3 + "' else @@IDENTITY end";
            identity = DB.getFieldValue(strSql);
            if (identity.Trim() == "")
            {
                return -6;
            }
            return 0;
        }

        public static void SetControlValue(Control control, TableInfo tableInfo)
        {
            UI.SetControlValue(tableInfo.FieldValueList, control);
        }

        public static int Update(TableInfo tableInfo, string keyValue)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            CheckTableInfo(tableInfo);
            string str = string.Empty;
            foreach (KeyValuePair<string, string> pair in tableInfo.FieldValueList)
            {
                string str3 = str;
                str = str3 + pair.Key.ToString() + "=" + CastValue(pair.Value) + ",";
            }
            str = str.Trim().TrimEnd(new char[] { ',' });
            if (!DB.execSql("update " + tableInfo.TableName + " set " + str + (keyValue.Trim().Equals("") ? "" : (" where " + tableInfo.KeyField + "='" + Safe.sqlStr(keyValue) + "'"))))
            {
                return -6;
            }
            return 0;
        }

        public static int UpdateWhere(TableInfo tableInfo, string _UpdateWhere)
        {
            if ((tableInfo.TableName == null) || tableInfo.TableName.Trim().Equals(""))
            {
                return -1;
            }
            CheckTableInfo(tableInfo);
            if ((_UpdateWhere != null) && (_UpdateWhere != ""))
            {
                _UpdateWhere = !_UpdateWhere.Trim().Substring(0, 5).ToLower().Equals("where ") ? (" where " + _UpdateWhere) : _UpdateWhere;
            }
            string str = string.Empty;
            foreach (KeyValuePair<string, string> pair in tableInfo.FieldValueList)
            {
                string str3 = str;
                str = str3 + pair.Key.ToString() + "=" + CastValue(pair.Value) + ",";
            }
            str = str.Trim().TrimEnd(new char[] { ',' });
            if (!DB.execSql("update " + tableInfo.TableName + " set " + str + _UpdateWhere))
            {
                return -6;
            }
            return 0;
        }
    }
}

